﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;
using Facade;
using System.Collections;

namespace IronOne.SalesOne.SalesOneWeb.Structure
{
    public partial class AddUnit : System.Web.UI.Page
    {
        //private String action = "Create";
        private String action = "View";
        //private String action = "Delete";
        private int successDel;
        private int successUpd;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (action == "Create")
            {
                LblHeader.Text = "Create Unit";
                BtnDone.Text = "Create";



                //FromValidator.Enabled = false;
                //ToValidator.Enabled = false;
                //CompareValidator.Enabled = false;
                //ErrorMessage.Visible = false;

            }
            else if (action == "Update")
            {
                LblHeader.Text = "Update Unit";
                BtnDone.Text = "Update";
                FillDropDown("Group"); 

                string unitStr = Session["/Structure/ManageUnits.aspxview"].ToString();
                int unitID = int.Parse(unitStr);
                StructureFacade unitFecade = new StructureFacade();
                SalesOneUnit sou = new SalesOneUnit();

                sou = unitFecade.GetUnit(unitID);

                txtCode.Text = sou.Code;
                txtName.Text = sou.Name;
                txtEffectiveFrom.Text = sou.From.ToString();
                txtEffectiveTo.Text = sou.To.ToString();

            }
            else if (action == "View")
            {
                LblHeader.Text = "View Unit";
                drpGroup.Visible = false;
                drpBranch.Visible = false;
                txtCode.Visible = false;
                txtName.Visible = false;
                txtEffectiveFrom.Visible = false;
                txtEffectiveTo.Visible = false;
                FillDropDown("Group"); 

                string unitStr = Session["/Structure/ManageUnits.aspxview"].ToString();
                int unitID = int.Parse(unitStr);
                SalesOneUnit unt = new SalesOneUnit();
                StructureFacade sf = new StructureFacade();
                unt = sf.GetUnit(unitID);

                //lblGroup.Text=unt.
                //lblBranch.text=
                FieldLblCode.Text = unt.Code;
                FieldLblName.Text = unt.Name;
                FieldLblFrom.Text = unt.From.ToString();
                FieldLblTo.Text = unt.To.ToString();
            }
            else if (action == "Delete")
            {
                FillDropDown("Group"); 
                string unitStr = Session["/Structure/ManageUnits.aspxview"].ToString();
                int unitID = int.Parse(unitStr);
                SalesOneUnit sou = new SalesOneUnit();
                StructureFacade strFacade = new StructureFacade();

                sou = strFacade.GetUnit(unitID);
                successDel = strFacade.DeleteUnit(sou);

                if (successDel == 1)
                {
                    Response.Redirect("~/Structure/ManageUnits.aspx");
                }

            }



        }



        protected void BtnDone_Click(object sender, EventArgs e)
        {
            if (action == "Create")
            {
                //FromValidator.Enabled = false;
                //ToValidator.Enabled = false;
                //CompareValidator.Enabled = false;
                //ErrorMessage.Visible = false;

                SalesOneUnit unit = new SalesOneUnit();
                unit.BranchId = drpBranch.SelectedIndex;
                unit.BranchId = 36;


                //unit.BranchId = drpBranch.SelectedIndex;

                unit.Code = txtCode.Text;
                unit.Name = txtName.Text;
                unit.From = DateTime.Parse(txtEffectiveFrom.Text);
                unit.To = DateTime.Parse(txtEffectiveTo.Text);

                StructureFacade unitFacade = new StructureFacade();
                unitFacade.InsertUnit(unit);


            }
            else if (action == "Update")
            {

                string unitStr = Session["/Structure/ManageUnits.aspxview"].ToString();
                int unitID = int.Parse(unitStr);
                SalesOneUnit sou = new SalesOneUnit();
                StructureFacade sf = new StructureFacade();
                sou = sf.GetUnit(unitID);

                successUpd = sf.UpdateUnit(sou);

                if (successUpd == 1)
                {
                    Response.Redirect("~/Structure/ManageUnits.aspx");
                }
                else
                {
                    //txtAddress2.Text = successUpd.ToString();
                }

            }

        }

        protected void drpGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillDropDown("Branch");            
        }

        public void FillDropDown(string type)
        {
            StructureFacade fac = new StructureFacade();

            switch (type)
            {
                case "Group":
                    List<SalesOneGroup> grp = fac.GetGroups("GroupId ASC");
                    SalesOneGroup grsel = new SalesOneGroup();
                    grsel.GroupId = -1;
                    grsel.Name = "Select";
                    grp.Insert(0, grsel);
                    drpGroup.DataSource = grp;
                    drpGroup.DataTextField = "Name";
                    drpGroup.DataValueField = "GroupId";
                    drpGroup.DataBind();
                    break;

                case "Branch":
                    if (drpGroup.SelectedValue != "-1")
                    {
                        List<Branch> br = fac.GetBranchesForGroup(Convert.ToInt32(drpGroup.SelectedValue));
                        drpBranch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select";
                        br.Insert(0, brSel);
                        drpBranch.DataTextField = "BranchName";
                        drpBranch.DataValueField = "BranchId";
                        drpBranch.DataBind();
                    }
                    break;
                
                default:
                    break;
            }
        }


    }
}

